package com.lt.flashcard.view;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.apache.log4j.Logger;
import org.bson.types.ObjectId;
import org.hibernate.Criteria;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.zkoss.bind.annotation.BindingParam;
import org.zkoss.bind.annotation.Command;
import org.zkoss.bind.annotation.Init;
import org.zkoss.zkplus.hibernate.HibernateUtil;
import org.zkoss.zul.Messagebox;

import com.google.code.morphia.Datastore;
import com.google.code.morphia.dao.BasicDAO;
import com.google.code.morphia.query.Query;
import com.lt.flashcard.TextUtil;
import com.lt.flashcard.Utility;
import com.lt.flashcard.model.entity.Flashcard;
import com.lt.flashcard.model.entity.FlashcardSet;
import com.lt.flashcard.model.entity.JosMedalAchievement;
import com.lt.flashcard.model.entity.JosMedalInfo;
import com.lt.flashcard.model.entity.JosUserPart;
import com.lt.flashcard.model.entity.Journey;
import com.lt.flashcard.model.entity.JourneyItem;
import com.lt.flashcard.model.entity.Medal;
import com.lt.flashcard.model.entity.MedalHistory;
import com.lt.flashcard.model.entity.Orderdetail;
import com.lt.flashcard.model.entity.Orderproduct;
import com.lt.flashcard.model.entity.Partner;
import com.lt.flashcard.model.entity.ProductCode;
import com.lt.flashcard.model.entity.Productcategory;
import com.lt.flashcard.model.entity.Promotion;
import com.lt.flashcard.model.entity.Role;
import com.lt.flashcard.model.entity.User;
import com.lt.flashcard.model.service.AAService;
import com.lt.flashcard.model.service.EcommerceService;
import com.lt.flashcard.model.service.EmailService;
import com.lt.flashcard.model.service.FlashcardSetService;
import com.lt.flashcard.model.service.LearningService;
import com.lt.flashcard.model.service.NotificationService;
import com.lt.flashcard.model.service.Repository;
import com.lt.flashcard.model.service.UserService;

public class ImportVm {

    private static final Logger log = Logger.getLogger(ImportVm.class);

    @Init
    public void init() {
        AAService asrv = Repository.getInstance().getAAService();
        if (!asrv.isLogin()) {
            Utility.loginOrAccessDenied(false);
        } else if (!asrv.hasRole(Role.ROLE_ADMIN)) {
            Utility.displayAccessDenied(false);
        }
    }

    @Command
    public void deletemedal() {
        LearningService lsrv = Repository.getInstance().getLearningService();
        List<Medal> lst = lsrv.getMedals();
        for (Medal m : lst) {
            lsrv.deleteMedal(m);
        }
        Messagebox.show("Finish");
    }

    /**
     * Import medal data to system
     */
    @Command
    public void importmedal() {
        Repository repo = Repository.getInstance();
        LearningService lsrv = Repository.getInstance().getLearningService();
        // BLUEUP'S LOVER
        Medal medal1 = new Medal();
        /*
         * medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S LOVER COPPER");
         * medal1.setCondition("Online liên tục (2, 7, 31)"); medal1.setDescription("Online liên tục (2, 7, 31)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/calendarcopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/calendarcopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/calendarcopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S LOVER SILVER");
         * medal1.setCondition("Online liên tục (2, 7, 31)"); medal1.setDescription("Online liên tục (2, 7, 31)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/loversilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/loversilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/loversilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S LOVER GOLD");
         * medal1.setCondition("Online liên tục (2, 7, 31)"); medal1.setDescription("Online liên tục (2, 7, 31)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/lovergold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/lovergoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/lovergoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BLUEUP'S DATE medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S DATE COPPER");
         * medal1.setCondition("Online lần thứ (11, 111, 555)"); medal1.setDescription("Online lần thứ (11, 111, 555)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/datecopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/datecopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/datecopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S DATE SILVER");
         * medal1.setCondition("Online lần thứ (11, 111, 555)"); medal1.setDescription("Online lần thứ (11, 111, 555)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/datesilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/datesilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/datesilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BLUEUP'S DATE GOLD");
         * medal1.setCondition("Online lần thứ (11, 111, 555)"); medal1.setDescription("Online lần thứ (11, 111, 555)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/dategold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/dategoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/dategoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BUSY BEE medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BUSY BEE COPPER");
         * medal1.setCondition("Làm KT chăm chỉ (3, 10, 60 ngày)");
         * medal1.setDescription("Làm KT chăm chỉ (3, 10, 60 ngày)"); medal1.setLargeThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beecopper.png")).getId().toString());
         * medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/beecopperthumbnail.png")).getId().toString()); medal1.setFocusThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beecopperthumbnailhighlight.png")).getId().toString());
         * lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BUSY BEE SILVER");
         * medal1.setCondition("Làm KT chăm chỉ (3, 10, 60 ngày)");
         * medal1.setDescription("Làm KT chăm chỉ (3, 10, 60 ngày)"); medal1.setLargeThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beesilver.png")).getId().toString());
         * medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/beesilverthumbnail.png")).getId().toString()); medal1.setFocusThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beesilverthumbnailhighlight.png")).getId().toString());
         * lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BUSY BEE GOLD");
         * medal1.setCondition("Làm KT chăm chỉ (3, 10, 60 ngày)");
         * medal1.setDescription("Làm KT chăm chỉ (3, 10, 60 ngày)"); medal1.setLargeThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beegold.png")).getId().toString());
         * medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/beegoldthumbnail.png")).getId().toString()); medal1.setFocusThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/beegoldthumbnailhighlight.png")).getId().toString());
         * lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BROADCASTER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BROADCASTER COPPER");
         * medal1.setCondition("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setDescription("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastercopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/broadcastercopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastercopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BROADCASTER SILVER");
         * medal1.setCondition("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setDescription("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastersilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/broadcastersilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastersilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("BROADCASTER GOLD");
         * medal1.setCondition("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setDescription("Tổng số view cho sản phẩm tự tạo (100, 5000, 50000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastergold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/broadcastergoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/broadcastergoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //SOCIALIST medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("SOCIALIST COPPER");
         * medal1.setCondition("Kết bạn (7, 100, 500 bạn)"); medal1.setDescription("Kết bạn (7, 100, 500 bạn)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/friendcopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/friendcopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/friendcopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("SOCIALIST SILVER");
         * medal1.setCondition("Kết bạn (7, 100, 500 bạn)"); medal1.setDescription("Kết bạn (7, 100, 500 bạn)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/socialsilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/socialsilverthumnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/socialsilverthumnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("SOCIALIST GOLD");
         * medal1.setCondition("Kết bạn (7, 100, 500 bạn)"); medal1.setDescription("Kết bạn (7, 100, 500 bạn)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/socialgold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/socialgoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/socialgoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //CONTRIBUTOR medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("CONTRIBUTOR COPPER");
         * medal1.setCondition("Tạo flashcard (50, 300, 1000)"); medal1.setDescription("Tạo flashcard (50, 300, 1000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/createcopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/createcopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/createcopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("CONTRIBUTOR SILVER");
         * medal1.setCondition("Tạo flashcard (50, 300, 1000)"); medal1.setDescription("Tạo flashcard (50, 300, 1000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/contrisilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/contrisilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/contrisilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_ACTIVITY); medal1.setName("CONTRIBUTOR GOLD");
         * medal1.setCondition("Tạo flashcard (50, 300, 1000)"); medal1.setDescription("Tạo flashcard (50, 300, 1000)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/contrigold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/contrigoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/contrigoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BLUEUP'S DOCTOR medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BLUEUP'S DOCTOR COPPER");
         * medal1.setCondition("Feedback (1, 5, 11)"); medal1.setDescription("Feedback (1, 5, 11)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorcop.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/doctorcopthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorcopthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BLUEUP'S DOCTOR SILVER");
         * medal1.setCondition("Feedback (1, 5, 11)"); medal1.setDescription("Feedback (1, 5, 11)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorsilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/doctorsilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorsilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BLUEUP'S DOCTOR GOLD");
         * medal1.setCondition("Feedback (1, 5, 11)"); medal1.setDescription("Feedback (1, 5, 11)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorgold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/doctorgoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/doctorgoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //ARCHER medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("ARCHER COPPER");
         * medal1.setCondition("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setDescription("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archercopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/archercopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archercopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("ARCHER SILVER");
         * medal1.setCondition("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setDescription("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archersilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/archersilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archersilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("ARCHER GOLD");
         * medal1.setCondition("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setDescription("Làm đúng (100 từ qua 0, 200 qua 1, 500 từ qua 7)");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archergold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/archergoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/archergoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //SHOPAHOLIC medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("SHOPAHOLIC COPPER");
         * medal1.setCondition("Đạt số Item"); medal1.setDescription("Đạt số Item");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopcopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/shopcopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopcopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("SHOPAHOLIC SILVER");
         * medal1.setCondition("Đạt số Item"); medal1.setDescription("Đạt số Item");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopsilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/shopsilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopsilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("SHOPAHOLIC GOLD");
         * medal1.setCondition("Đạt số Item"); medal1.setDescription("Đạt số Item");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopgold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/shopgoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/shopgoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BOUNTY HUNTER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BOUNTY HUNTER COPPER");
         * medal1.setCondition("Làm đúng các challenge"); medal1.setDescription("Làm đúng các challenge");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountycopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/bountycopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountycopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BOUNTY HUNTER SILVER");
         * medal1.setCondition("Làm đúng các challenge"); medal1.setDescription("Làm đúng các challenge");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountysilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/bountysilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountysilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * medal1 = new Medal(); medal1.setCategory(Medal.CATEGORY_STUDY); medal1.setName("BOUNTY HUNTER GOLD");
         * medal1.setCondition("Làm đúng các challenge"); medal1.setDescription("Làm đúng các challenge");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountygold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/bountygoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/bountygoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //WELCOME medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("WELCOME");
         * medal1.setCondition("Đăng nhập lần đầu"); medal1.setDescription("Đăng nhập lần đầu");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/WELCOME.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/WELCOME - thumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/WELCOME - thumbnail - highlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //BLUEUP'S GURU medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("BLUEUP'S GURU");
         * medal1.setCondition("Đăng nhập lần đầu"); medal1.setDescription("Đăng nhập lần đầu");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/guru.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/guruthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/guruthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //HATTRICK SCORER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("HATTRICK SCORER");
         * medal1.setCondition("Làm đúng tất cả trong một lần kiểm tra 0 1 7");
         * medal1.setDescription("Làm đúng tất cả trong một lần kiểm tra 0 1 7");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/hattrick.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/hattrickthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/hattrickthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //SUPERMAN medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("SUPERMAN");
         * medal1.setCondition("Làm đúng tất cả trong một lần kiểm tra toàn bộ");
         * medal1.setDescription("Làm đúng tất cả trong một lần kiểm tra toàn bộ");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/superman.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/supermanthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/supermanthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //EXPLORER medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("EXPLORER");
         * medal1.setCondition("Kích hoạt set Blueup lần đầu tiên");
         * medal1.setDescription("Kích hoạt set Blueup lần đầu tiên"); medal1.setLargeThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/fisrtactivation.png")).getId().toString());
         * medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/fisrtactivationthumbnail.png")).getId().toString()); medal1.setFocusThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/fisrtactivationthumbnailhighlight.png")).getId().toString());
         * lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //WOMEN'S DAY medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("WOMEN'S DAY");
         * medal1.setCondition("Offline 8/3"); medal1.setDescription("Offline 8/3");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/womenday.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/womendaythumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/womendaythumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //EARLY BIRD medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("EARLY BIRD");
         * medal1.setCondition("early bird: dành cho sự kiện đăng ký tham gia hệ thống Blueup mới");
         * medal1.setDescription("early bird: dành cho sự kiện đăng ký tham gia hệ thống Blueup mới");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/earlybird.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/earlybirdthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/earlybirdthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //FORTUNE medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("FORTUNE");
         * medal1.setCondition("Clover: Medal may mắn"); medal1.setDescription("Clover: Medal may mắn");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/fortune.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/fortunethumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/fortunethumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //HAPPY BIRTHDAY medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("HAPPY BIRTHDAY");
         * medal1.setCondition("HAPPY BIRTHDAY"); medal1.setDescription("HAPPY BIRTHDAY");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/HAPPYBIRTHDAY.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/HAPPYBIRTHDAYthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/HAPPYBIRTHDAYthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //HAPPY NEW YEAR 2013 medal1 =
         * new Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("HAPPY NEW YEAR 2013");
         * medal1.setCondition("HAPPY NEW YEAR 2013"); medal1.setDescription("HAPPY NEW YEAR 2013");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/2013.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/2013thumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/2013thumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //NHA TRANG BEACH medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("NHA TRANG BEACH");
         * medal1.setCondition("NHA TRANG BEACH"); medal1.setDescription("NHA TRANG BEACH");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/nhatrangbeach.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/nhatrangbeachthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/nhatrangbeachthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //TRIAL medal1 = new Medal();
         * medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("TRIAL"); medal1.setCondition("TRIAL");
         * medal1.setDescription("TRIAL"); medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath()
         * + "import/trial.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/trialthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trialthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //TIME RACER COPPER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("TIME RACER COPPER");
         * medal1.setCondition("TIME RACER COPPER"); medal1.setDescription("TIME RACER COPPER");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trcopper.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/trcopperthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trcopperthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //TIME RACER SILVER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("TIME RACER SILVER");
         * medal1.setCondition("TIME RACER SILVER"); medal1.setDescription("TIME RACER SILVER");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trsilver.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/trsilverthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trsilverthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         * 
         * //------------------------------------------------------------------------- //TIME RACER SILVER medal1 = new
         * Medal(); medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT); medal1.setName("TIME RACER GOLD");
         * medal1.setCondition("TIME RACER GOLD"); medal1.setDescription("TIME RACER GOLD");
         * medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trgold.png")).getId().toString()); medal1.setSmallThumbnail(repo.saveBinaryData(new
         * File(Utility.getWebAppPath() + "import/trgoldthumbnail.png")).getId().toString());
         * medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() +
         * "import/trgoldthumbnailhighlight.png")).getId().toString()); lsrv.saveMedal(medal1);
         */

        // -------------------------------------------------------------------------
        // CHILDREN'S DAY
        /*medal1 = new Medal();
        medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT);
        medal1.setName("CHILDREN'S DAY");
        medal1.setCondition("");
        medal1.setDescription("CHILDREN'S DAY");
        medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/children.png")).getId().toString());
        medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/childrenthumbnail.png")).getId().toString());
        medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/childrenthumbnailhighlight.png")).getId().toString());
        lsrv.saveMedal(medal1);*/

        medal1 = new Medal();
        medal1.setCategory(Medal.CATEGORY_ACHIEVEMENT);
        medal1.setName("CHILDREN'S DAY");
        medal1.setCondition("");
        medal1.setDescription("CHILDREN'S DAY");
        medal1.setLargeThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/children.png"));
        medal1.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/childrenthumbnail.png"));
        medal1.setFocusThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/childrenthumbnailhighlight.png"));
        lsrv.saveMedal(medal1);

        Messagebox.show("Finish");
    }

    /**
     * Get a medal
     */
    @Command
    public void getmedal() {
        LearningService lsrv = Repository.getInstance().getLearningService();
        Medal medal = lsrv.getMedals().get(0);
        lsrv.userGetMedal(Utility.getUser(), medal);

        Messagebox.show("Finish");
    }

    /**
     * Import medal achievement result
     */
    @Command
    public void gainmedal() {
        Session session = HibernateUtil.currentSession();
        UserService usrv = Repository.getInstance().getUserService();
        LearningService lsrv = Repository.getInstance().getLearningService();
        BasicDAO<MedalHistory, ObjectId> udao = new BasicDAO<MedalHistory, ObjectId>(MedalHistory.class, Repository.getInstance().getDatastore());

        List<Integer> failIds = new ArrayList<Integer>();

        Criteria crit = session.createCriteria(JosMedalAchievement.class);
        List<JosMedalAchievement> ls = crit.list();
        for (JosMedalAchievement a : ls) {
            User u = usrv.findByEmail(a.getUseremail());
            Medal m = lsrv.getMedal(a.getMedalmongoid());
            if (u != null && m != null) {
                MedalHistory his = new MedalHistory();
                his.setMedal(m);
                his.setUserid(u.getId());
                his.setCreated(a.getAchieveDate());
                udao.save(his);
            } else {
                failIds.add(a.getId());
            }
        }

        Messagebox.show("Finish");
    }

    @Command
    public void indexmedal() {
        LearningService lsrv = Repository.getInstance().getLearningService();
        Session session = HibernateUtil.currentSession();

        Criteria crit = session.createCriteria(JosMedalInfo.class);
        List<JosMedalInfo> mdlst = crit.list();
        for (JosMedalInfo info : mdlst) {
            Medal medal = lsrv.getMedalByName(info.getMedalName());
            if (medal != null) {
                String update = "update JosMedalInfo set mongoid = :newid where id = :curid";
                session.createQuery(update).setString("newid", medal.getId().toString()).setInteger("curid", info.getId()).executeUpdate();
            }
        }

        Messagebox.show("Finish");
    }

    /**
     * Delete journey
     */
    @Command
    public void deletejourney() {
        LearningService lsrv = Repository.getInstance().getLearningService();
        List<Journey> lst = lsrv.getAllJourneys();
        for (Journey j : lst) {
            lsrv.deleteJourney(j);
        }

        Messagebox.show("Finish");
    }

    @Command
    public void importjourney() {
        Repository repo = Repository.getInstance();
        LearningService srv = Repository.getInstance().getLearningService();

        Journey j = new Journey();
        j.setDashboardBackground(Utility.saveImage(Utility.getWebAppPath() + "images/bg_dashboard_nhatrang.jpg"));
        j.setDescription("Nha Trang beach journey");
        j.setLargeThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/photo_journey.png"));
        j.setName("Nha Trang beach");
        j.setNameThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/nha_trang_beach.png"));
        j.setProfileBackground(Utility.saveImage(Utility.getWebAppPath() + "images/nhatrangbeach.png"));
        j.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/photo_journey_small.png"));
        j.setTotalcoin(5460);

        List<JourneyItem> items = new ArrayList<JourneyItem>();
        JourneyItem item1 = new JourneyItem();
        item1.setCoin(240);
        item1.setName("Sun cream");
        item1.setPosLargeGetit("145,385");
        item1.setPosMediumGetit("130,155");
        item1.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sun_journey_hover.png"));
        item1.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sun_journey.png"));

        JourneyItem item2 = new JourneyItem();
        item2.setCoin(2000);
        item2.setName("Dive glasses");
        item2.setPosLargeGetit("225,577");
        item2.setPosMediumGetit("369,106");
        item2.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/dive_journey_hover.png"));
        item2.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/dive_journey.png"));

        JourneyItem item3 = new JourneyItem();
        item3.setCoin(3220);
        item3.setChallenge(true);
        item3.setName("Beach Suit");
        item3.setPosLargeGetit("314,300");
        item3.setPosMediumGetit("717,78");
        item3.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/suit_journey_hover.png"));
        item3.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/suit_journey.png"));

        items.add(item1);
        items.add(item2);
        items.add(item3);
        j.setItems(items);

        srv.saveJourney(j);

        Messagebox.show("Finish");
    }

    @Command
    public void importjourneysapa() {
        Repository repo = Repository.getInstance();
        LearningService srv = Repository.getInstance().getLearningService();

        Journey j = new Journey();
        // sapa journey
        j = new Journey();
        j.setDashboardBackground(Utility.saveImage(Utility.getWebAppPath() + "images/sapa_dashboard.jpg"));
        j.setDescription("Sapa journey");
        j.setLargeThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sapa_largethumbnail.png"));
        j.setName("SAPA");
        j.setNameThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sapa.png"));
        j.setProfileBackground(Utility.saveImage(Utility.getWebAppPath() + "images/sapabg.png"));
        j.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sapa_smallthumbnail.png"));
        j.setTotalcoin(10140);

        List<JourneyItem> items = new ArrayList<JourneyItem>();
        JourneyItem item1 = new JourneyItem();
        item1.setCoin(1600);
        item1.setName("Tho Cam bag");
        item1.setPosLargeGetit("142,533");
        item1.setPosMediumGetit("138,171");
        item1.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/tui tho cam - thumbnail - no color.png"));
        item1.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/tui tho cam - thumbnail.png"));

        JourneyItem item2 = new JourneyItem();
        item2.setCoin(3240);
        item2.setName("Com Lam");
        item2.setPosLargeGetit("277,383");
        item2.setPosMediumGetit("486,132");
        item2.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/com lam - thumbnail - no color.png"));
        item2.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/com lam - thumbnail.png"));

        JourneyItem item3 = new JourneyItem();
        item3.setCoin(5300);
        item3.setChallenge(true);
        item3.setName("Sapa Doll");
        item3.setPosLargeGetit("306,269");
        item3.setPosMediumGetit("736,78");
        item3.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/bup be Sapa - thumnail - no color.png"));
        item3.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/bup be Sapa - thumnail.png"));

        items.add(item1);
        items.add(item2);
        items.add(item3);
        j.setItems(items);

        srv.saveJourney(j);

        Messagebox.show("Finish");
    }

    @Command
    public void importflashcardset() {
        Session ses = HibernateUtil.currentSession();
        Criteria crit = ses.createCriteria(JosUserPart.class);
        crit.add(Restrictions.eq("processed", 0));
        crit.setMaxResults(100);
        List<JosUserPart> lst = crit.list();

        UserService usrv = Repository.getInstance().getUserService();
        FlashcardSetService fsrv = Repository.getInstance().getFlashcardSetService();
        List<Integer> fails = new ArrayList<Integer>();

        Map<Long, FlashcardSet> cache = new HashMap<Long, FlashcardSet>();

        int i = 0;
        for (JosUserPart p : lst) {
            i++;
            if (Utility.isEmptyString(p.getEmail()) || p.getNewid() == 0) {
                fails.add(p.getId());
                p.setProcessed(-1);
            } else {
                User user = usrv.findByEmail(p.getEmail());
                Long id = new Long(p.getNewid());
                FlashcardSet fcs = cache.containsKey(id) ? cache.get(id) : fsrv.findByID(new Long(p.getNewid()));
                if (user == null || fcs == null) {
                    fails.add(p.getId());
                    p.setProcessed(-1);
                } else {
                    FlashcardSet nfcs = fcs.clone();
                    nfcs.setAuthor(user);
                    nfcs.setAdmin(FlashcardSet.ADMIN_BUY);
                    nfcs.setPaymenttype(Orderdetail.TYPE_COIN);
                    fsrv.save(nfcs);
                    p.setProcessed(1);
                }
            }
            ses.save(p);
        }
        Utility.redirect("import.zul", true);
    }

    @Command
    public void importlearning() {
        Messagebox.show("Import via sql");
    }

    @Command
    public void deletecode() {
        Datastore ds = Repository.getInstance().getDatastore();
        Query<ProductCode> query = ds.createQuery(ProductCode.class);
        ds.delete(query);

        Messagebox.show("Finish");
    }

    @Command
    public void importcode() {
        /*
         * Session ses = HibernateUtil.currentSession(); Criteria crit = ses.createCriteria(JosCode.class); //modified
         * to just import previous fail processed and just for IELTS 7 crit.add(Restrictions.eq("processed", -1));
         * crit.add(Restrictions.eq("name", "Tiếng Anh 12 - Học kỳ 1")); crit.setMaxResults(5000); List<JosCode> lst =
         * crit.list();
         * 
         * EcommerceService srv = Repository.getInstance().getEcommerceService(); UserService usrv =
         * Repository.getInstance().getUserService();
         * 
         * for(JosCode j : lst) { if(j.getNewid() == 0 || Utility.isEmptyString(j.getName())) { j.setProcessed(-11);
         * ses.save(j); continue; } ProductCode code = new ProductCode(); code.setCode(j.getCode());
         * code.setCreated(j.getGenDate()); code.setProductId(new Long(j.getNewid())); code.setProductName(j.getName());
         * code.setStatus(j.getStatus() == 1 ? ProductCode.STATUS_USED : ProductCode.STATUS_NEW);
         * 
         * if(j.getStatus() == 1) { User user = usrv.findByEmail(j.getEmail());
         * 
         * if(user == null) { j.setProcessed(-11); ses.save(j); continue; }
         * 
         * ProductCodeHistory his = new ProductCodeHistory(); his.setUsedIp("0.0.0.0");
         * his.setUsedOn(j.getActivateDate()); his.setUserId(user.getId()); his.setUserName(user.getUsername());
         * his.setNewProductId(0l); code.addUseHistory(his); }
         * 
         * srv.saveProductCode(code); j.setProcessed(11); ses.save(j); }
         * 
         * Messagebox.show("Finish");
         */
    }

    @Command
    public void gainmedalnewyear() {
        Session ses = HibernateUtil.currentSession();
        org.hibernate.Query query = ses.createQuery("from User");

        LearningService srv = Repository.getInstance().getLearningService();
        Medal medal = srv.getMedalByName("HAPPY NEW YEAR 2013");

        BasicDAO<MedalHistory, ObjectId> udao = new BasicDAO<MedalHistory, ObjectId>(MedalHistory.class, Repository.getInstance().getDatastore());

        NotificationService nsrv = Repository.getInstance().getNotificationService();

        int count = 0;

        while (query.iterate().hasNext()) {
            User user = (User) query.iterate().next();

            MedalHistory his = new MedalHistory();
            his.setMedal(medal);
            his.setUserid(user.getId());
            udao.save(his);

            nsrv.addNotificationGetMedal(user, medal);

            count++;

        }
    }

    /**
     * Send email to reset password
     */
    @Command
    public void resetpassword() {
        Session ses = HibernateUtil.currentSession();
        Criteria crit = ses.createCriteria(User.class);
        crit.add(Restrictions.eq("activation", ""));
        crit.add(Restrictions.eq("password", ""));
        crit.setMaxResults(500);
        List<User> lst = crit.list();

        if (lst.size() > 0) {
            EmailService srv = Repository.getInstance().getEmailService();

            for (User user : lst) {
                UUID idOne = UUID.randomUUID();
                user.setActivation(idOne.toString());
                Calendar cal = Calendar.getInstance();
                cal.setTime(new Date());
                cal.add(Calendar.DATE, 30);
                user.setExpiredate(cal.getTime());// expire date
                ses.update(user);

                srv.sendEmailNewSystem(user.getUsername(), user.getEmail(), user.getActivation());
            }
        }

        Messagebox.show("Finish");
    }

    @Command
    public void promotion() {
        EcommerceService srv = Repository.getInstance().getEcommerceService();
        LearningService msrv = Repository.getInstance().getLearningService();
        Promotion pro;
        Medal medal;

        /*medal = msrv.getMedalByName("ARCHER COPPER");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_KNOWWORDS_L0);
        pro.setCondition_value(100);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("ARCHER SILVER");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_KNOWWORDS_L1);
        pro.setCondition_value(200);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("ARCHER GOLD");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_KNOWWORDS_L7);
        pro.setCondition_value(500);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("WELCOME");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_FIRSTLOGIN);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("BUSY BEE COPPER");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_TESTCONTINUOUS);
        pro.setCondition_value(3);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("BUSY BEE SILVER");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_TESTCONTINUOUS);
        pro.setCondition_value(10);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);
        
        medal = msrv.getMedalByName("BUSY BEE GOLD");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_TESTCONTINUOUS);
        pro.setCondition_value(60);
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);*/

        medal = msrv.getMedalByName("CHILDREN'S DAY");
        pro = new Promotion();
        pro.setTargetType(Promotion.TARGETTYPE_USER);
        pro.setConditiontype(Promotion.CONDITIONTYPE_LOGINAT);
        Calendar cal = Calendar.getInstance();
        cal.set(2013, 6, 1);
        pro.setCondition_date(cal.getTime());
        pro.setType(Promotion.TYPE_GAINMEDAL);
        pro.setMedalid(medal.getId().toString());
        srv.savePromotion(pro);

        Messagebox.show("Finish add promotion for gainmedal");
    }

    /**
     * Add blank flashcardset to store
     */
    @Command
    public void addblankflashcard() {
        FlashcardSetService srv = Repository.getInstance().getFlashcardSetService();
        UserService usrv = Repository.getInstance().getUserService();
        EcommerceService esrv = Repository.getInstance().getEcommerceService();

        Productcategory cat = esrv.getProductCategoryByName("blank");// blank category
        if (cat == null) {
            cat = new Productcategory();
            cat.setName("blank");
            esrv.saveProductCategory(cat);
        }

        User admin = usrv.findByUsername("admin");
        FlashcardSet set = new FlashcardSet();
        set.setAdmin(FlashcardSet.ADMIN_YES);
        set.setAuthor(admin);
        set.setDescription("Lorem ipsum");
        set.setHaspaper((byte) 0);
        set.setName("Blank Flashcard");
        set.setPriceEflashcard(15000);
        set.setReadysale((byte) 1);
        set.setTotalflashcards(100);
        set.setBlankflashcardset(1);
        set.setProductcategory(cat);
        srv.save(set);

        Messagebox.show("Finish");
    }

    @Command
    public void adminwelcom() {
        UserService usrv = Repository.getInstance().getUserService();
        LearningService lsrv = Repository.getInstance().getLearningService();
        BasicDAO<MedalHistory, ObjectId> udao = new BasicDAO<MedalHistory, ObjectId>(MedalHistory.class, Repository.getInstance().getDatastore());

        User u = Utility.getUser();
        Medal m = lsrv.getMedalByName("WELCOME");
        if (u != null && m != null) {
            MedalHistory his = new MedalHistory();
            his.setMedal(m);
            his.setUserid(u.getId());
            udao.save(his);
        }

        Messagebox.show("Finish");
    }

    @Command
    public void welcommedalforallusers() {
        Session ses = Repository.getInstance().getCurrentSession();
        LearningService lsrv = Repository.getInstance().getLearningService();
        BasicDAO<MedalHistory, ObjectId> udao = new BasicDAO<MedalHistory, ObjectId>(MedalHistory.class, Repository.getInstance()
                .getDatastore());

        Criteria crit = ses.createCriteria(User.class);
        ScrollableResults scroll = crit.scroll();
        Medal m = lsrv.getMedalByName("WELCOME");

        while (scroll.next()) {
            User user = (User) scroll.get(0);

            if (lsrv.checkUserGotMedal(user.getId(), m))
                continue;

            MedalHistory his = new MedalHistory();
            his.setMedal(m);
            his.setUserid(user.getId());
            udao.save(his);
        }

        scroll.close();

        Messagebox.show("Finish");
    }

    @Command
    public void fixMedalWelcom() {
        Repository repo = Repository.getInstance();
        LearningService lsrv = Repository.getInstance().getLearningService();
        /*Medal medal1 = lsrv.getMedalByName("WELCOME");
        
        repo.deleteBinaryData(medal1.getLargeThumbnail());
        repo.deleteBinaryData(medal1.getSmallThumbnail());
        repo.deleteBinaryData(medal1.getFocusThumbnail());
        
        medal1.setLargeThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/WELCOME.png")).getId().toString());
        medal1.setSmallThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/WELCOME - thumbnail.png")).getId().toString());
        medal1.setFocusThumbnail(repo.saveBinaryData(new File(Utility.getWebAppPath() + "import/WELCOME - thumbnail - highlight.png")).getId().toString());
        lsrv.saveMedal(medal1);*/

        Medal medal1 = lsrv.getMedalByName("WOMEN'S DAY");

        repo.deleteBinaryData(medal1.getLargeThumbnail());
        repo.deleteBinaryData(medal1.getSmallThumbnail());
        repo.deleteBinaryData(medal1.getFocusThumbnail());

        medal1.setLargeThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/832013.png"));
        medal1.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/832013-thumbnail.png"));
        medal1.setFocusThumbnail(Utility.saveImage(Utility.getWebAppPath() + "import/832013-thumbnail-highlight.png"));
        lsrv.saveMedal(medal1);

        Messagebox.show("Finish");
    }

    @Command
    public void fixJourneyNhatrang() {
        Repository repo = Repository.getInstance();
        LearningService srv = Repository.getInstance().getLearningService();

        Journey j = srv.getJourneyByName("Nha Trang beach");
        j.setDashboardBackground(Utility.saveImage(Utility.getWebAppPath() + "images/bg_dashboard_nhatrang.jpg"));
        j.setProfileBackground(Utility.saveImage(Utility.getWebAppPath() + "images/nhatrangbeach.png"));
        j.setTotalcoin(5460);

        for (JourneyItem item : j.getItems()) {
            repo.deleteBinaryData(item.getSmallGrayThumbnail());
            repo.deleteBinaryData(item.getSmallThumbnail());
        }

        List<JourneyItem> items = new ArrayList<JourneyItem>();
        JourneyItem item1 = new JourneyItem();
        item1.setCoin(240);
        item1.setName("Sun cream");
        item1.setPosLargeGetit("145,385");
        item1.setPosMediumGetit("130,155");
        item1.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sun_journey_hover.png"));
        item1.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/sun_journey.png"));

        JourneyItem item2 = new JourneyItem();
        item2.setCoin(2000);
        item2.setName("Dive glasses");
        item2.setPosLargeGetit("225,577");
        item2.setPosMediumGetit("369,106");
        item2.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/dive_journey_hover.png"));
        item2.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/dive_journey.png"));

        JourneyItem item3 = new JourneyItem();
        item3.setCoin(3220);
        item3.setChallenge(true);
        item3.setName("Beach Suit");
        item3.setPosLargeGetit("314,300");
        item3.setPosMediumGetit("717,78");
        item3.setSmallGrayThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/suit_journey_hover.png"));
        item3.setSmallThumbnail(Utility.saveImage(Utility.getWebAppPath() + "images/suit_journey.png"));

        items.add(item1);
        items.add(item2);
        items.add(item3);
        j.setItems(items);

        srv.saveJourney(j);

        Messagebox.show("Finish");
    }

    @Command
    public void getcode(@BindingParam("code") String code) {
        EcommerceService esrv = Repository.getInstance().getEcommerceService();
        ProductCode c = esrv.getProductCode(code);
        Messagebox.show(c == null ? "Cant find" : c.getCode());
    }

    @Command
    public void buyProduct(@BindingParam("order") Long orderid, @BindingParam("transaction") String transaction) {
        EcommerceService esrv = Repository.getInstance().getEcommerceService();
        FlashcardSetService fsrv = Repository.getInstance().getFlashcardSetService();
        UserService usrv = Repository.getInstance().getUserService();

        Orderdetail order = esrv.getOrderdetail(orderid);
        if (order == null) {
            Messagebox.show("Unknow order id " + orderid);
            return;
        }

        order.setStatus(Orderdetail.STATUS_COMPLETE);
        order.setTransactionid(transaction);
        // add the flashcardset
        for (Orderproduct item : order.getOrderproducts()) {
            FlashcardSet nfcs = item.getFlashcardset().clone();
            nfcs.setAuthor(order.getUser());
            nfcs.setAdmin(FlashcardSet.ADMIN_BUY);
            nfcs.setPaymenttype(Orderdetail.TYPE_NGANLUONG);
            item.setTargetset(nfcs);
            fsrv.save(nfcs);
            esrv.applyPromotions(order.getUser(),
                    esrv.getAllValidPromotions(Promotion.TARGETTYPE_FLASHCARDSET, Promotion.CONDITIONTYPE_BUYITEM, item.getFlashcardset().getId()),
                    true);
        }
        esrv.saveOrderdetail(order);

        // Add activity
        for (Orderproduct item : order.getOrderproducts()) {
            usrv.addActivityBuyItem(order.getUser(), item.getFlashcardset());
        }

        if (log.isInfoEnabled()) {
            log.info("Admin process order " + orderid + " with transaction " + transaction);
        }

        Messagebox.show("Finish");
    }

    /**
     * Scan flashcard and remove audio if not exists
     */
    @Command
    public void checkAudio() {
        Session ses = Repository.getInstance().getCurrentSession();
        Criteria crit = ses.createCriteria(Flashcard.class);
        crit.add(Restrictions.isNotNull("soundFront"));
        // crit.add(Restrictions.ne("soundFront", ""));

        String dir = Utility.getStreamFolder();

        ScrollableResults scroll = crit.scroll();
        while (scroll.next()) {
            Flashcard fc = (Flashcard) scroll.get(0);

            String sound = fc.getSoundFront();
            if (Utility.isEmptyString(sound)) {
                continue;
            }

            File file = new File(String.format("%s%s", dir, sound));
            if (!file.exists()) {
                fc.setSoundFront("");
                ses.save(fc);
                // log.info(String.format("File %s not exist", String.format("%s/%s", dir, sound)));
            }
        }

        scroll.close();

        Messagebox.show("Finish");
    }

    @Command
    public void addSampleCode() {
        ProductCode code = new ProductCode();
        code.setCode("1234");
        Map<Long, String> pro = new HashMap<Long, String>();
        pro.put(1000l, "Thong");
        code.setProducts(pro);
        Repository.getInstance().getEcommerceService().saveProductCode(code);
    }

    /**
     * Init first list of partners
     */
    @Command
    public void importPartner() {
        List<String[]> data = new ArrayList<String[]>(); // store list of partners, code, name
        data.add(new String[] { "TH", "Tôi tự học" });
        data.add(new String[] { "HD", "Hợp Điểm" });
        data.add(new String[] { "BP", "Breakthrough Power" });
        data.add(new String[] { "VI", "Viet Idea" });
        data.add(new String[] { "GM", "Global Manpower" });
        data.add(new String[] { "IA", "ILA" });
        data.add(new String[] { "VU", "VUS" });
        data.add(new String[] { "VA", "VAS" });
        data.add(new String[] { "TB", "Tây Bắc Mỹ" });
        data.add(new String[] { "MJ", "Major" });
        data.add(new String[] { "JC", "JCUB" });

        EcommerceService srv = Repository.getInstance().getEcommerceService();
        for (String[] obj : data) {
            Partner p = new Partner();
            p.setCode(obj[0]);
            p.setName(obj[1]);
            srv.savePartner(p);
        }

        Messagebox.show("Finish");
    }

    /**
     * Export all images in repository of flashcardset to data files
     */
    @Command
    public void exportImageOfFlashcardSet() {
        Session ses = Repository.getInstance().getCurrentSession();
        Criteria crit = ses.createCriteria(FlashcardSet.class);
        crit.add(Restrictions.not(Restrictions.like("image", ".", MatchMode.ANYWHERE)));
        crit.add(Restrictions.isNotNull("image"));
        crit.add(Restrictions.ne("image", ""));
        crit.setMaxResults(100000);

        ScrollableResults scroll = crit.scroll();
        List<Long> fails = new ArrayList<Long>();
        List<Long> success = new ArrayList<Long>();

        int batchsize = 50;
        int count = 0;

        while (scroll.next()) {
            FlashcardSet set = (FlashcardSet) scroll.get(0);

            String path = Utility.loadImageFromGridFS(set.getImage(), null);
            if (path == null) {
                fails.add(set.getId());
            } else {
                path = path.split("/")[1];
                set.setImage(path);
                ses.save(set);
                success.add(set.getId());
            }

            if (++count > batchsize) {
                ses.flush();
                ses.clear();
                count = 0;
            }
        }

        ses.flush();
        ses.clear();

        log.info("Sucess: " + success.toString());
        log.info("Fail: " + fails.toString());
        Messagebox.show("Finish: " + success.size());
    }

    /**
     * Export all images in repository of flashcards to data files
     */
    @Command
    public void exportImageOfFlashcard() {
        Session ses = Repository.getInstance().getCurrentSession();
        Criteria crit = ses.createCriteria(Flashcard.class);
        crit.add(Restrictions.not(Restrictions.like("image", ".", MatchMode.ANYWHERE)));
        crit.add(Restrictions.isNotNull("image"));
        crit.add(Restrictions.ne("image", ""));
        crit.setMaxResults(100000);

        ScrollableResults scroll = crit.scroll();
        // List<Long> fails = new ArrayList<Long>();
        List<Long> success = new ArrayList<Long>();

        int batchsize = 50;
        int count = 0;

        while (scroll.next()) {
            Flashcard set = (Flashcard) scroll.get(0);

            String path = Utility.loadImageFromGridFS(set.getImage(), null);
            if (path == null) {
                // fails.add(set.getId());
            } else {
                path = path.split("/")[1];
                set.setImage(path);
                ses.save(set);
                success.add(set.getId());
            }

            if (++count > batchsize) {
                ses.flush();
                ses.clear();
                count = 0;
            }
        }

        ses.flush();
        ses.clear();

        log.info("Sucess: " + success.toString());
        // log.info("Fail: " + fails.toString());
        Messagebox.show("Finish: " + success.size());
    }

    /**
     * Export all images in repository of users
     */
    @Command
    public void exportImageOfOthers() {
        Session ses = Repository.getInstance().getCurrentSession();

        int batchsize = 50;
        int count = 0;

        // convert users
        Criteria crituser = ses.createCriteria(User.class);
        crituser.add(Restrictions.not(Restrictions.like("avatar", ".", MatchMode.ANYWHERE)));
        crituser.add(Restrictions.isNotNull("avatar"));
        crituser.add(Restrictions.ne("avatar", ""));

        ScrollableResults scrolluser = crituser.scroll();

        while (scrolluser.next()) {
            User set = (User) scrolluser.get(0);

            String path = Utility.loadImageFromGridFS(set.getAvatar(), null);
            if (path == null) {
                // fails.add(set.getId());
            } else {
                path = path.split("/")[1];
                set.setAvatar(path);
                ses.save(set);
            }

            if (++count > batchsize) {
                ses.flush();
                ses.clear();
                count = 0;
            }
        }

        count = 0;
        ses.flush();
        ses.clear();

        // convert medal
        BasicDAO<Medal, ObjectId> _medalDAO = new BasicDAO<Medal, ObjectId>(Medal.class, Repository.getInstance().getDatastore());
        Query<Medal> query = _medalDAO.createQuery();
        for (Medal h : query.fetch()) {

            String path;
            if (!Utility.isEmptyString(h.getLargeThumbnail()) && !h.getLargeThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getLargeThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setLargeThumbnail(path);
                }
            }

            if (!Utility.isEmptyString(h.getSmallThumbnail()) && !h.getSmallThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getSmallThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setSmallThumbnail(path);
                }
            }

            if (!Utility.isEmptyString(h.getFocusThumbnail()) && !h.getFocusThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getFocusThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setFocusThumbnail(path);
                }
            }

            _medalDAO.save(h);
        }

        // convert journey
        BasicDAO<Journey, ObjectId> _DAO = new BasicDAO<Journey, ObjectId>(Journey.class, Repository.getInstance().getDatastore());
        Query<Journey> qu = _DAO.createQuery();
        for (Journey h : qu.fetch()) {

            String path;
            if (!Utility.isEmptyString(h.getDashboardBackground()) && !h.getDashboardBackground().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getDashboardBackground(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setDashboardBackground(path);
                }
            }

            if (!Utility.isEmptyString(h.getLargeThumbnail()) && !h.getLargeThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getLargeThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setLargeThumbnail(path);
                }
            }

            if (!Utility.isEmptyString(h.getNameThumbnail()) && !h.getNameThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getNameThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setNameThumbnail(path);
                }
            }

            if (!Utility.isEmptyString(h.getProfileBackground()) && !h.getProfileBackground().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getProfileBackground(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setProfileBackground(path);
                }
            }

            if (!Utility.isEmptyString(h.getSmallThumbnail()) && !h.getSmallThumbnail().contains(".")) {
                path = Utility.loadImageFromGridFS(h.getSmallThumbnail(), null);
                if (path == null) {
                    // fails.add(set.getId());
                } else {
                    path = path.split("/")[1];
                    h.setSmallThumbnail(path);
                }
            }

            for (JourneyItem item : h.getItems()) {
                if (!Utility.isEmptyString(item.getSmallThumbnail()) && !item.getSmallThumbnail().contains(".")) {
                    path = Utility.loadImageFromGridFS(item.getSmallThumbnail(), null);
                    if (path == null) {
                        // fails.add(set.getId());
                    } else {
                        path = path.split("/")[1];
                        item.setSmallThumbnail(path);
                    }
                }

                if (!Utility.isEmptyString(item.getSmallGrayThumbnail()) && !item.getSmallGrayThumbnail().contains(".")) {
                    path = Utility.loadImageFromGridFS(item.getSmallGrayThumbnail(), null);
                    if (path == null) {
                        // fails.add(set.getId());
                    } else {
                        path = path.split("/")[1];
                        item.setSmallGrayThumbnail(path);
                    }
                }
            }

            _DAO.save(h);
        }

        Messagebox.show("Finish");
    }

    @Command
    public void addSlugProductCategory() {
        com.lt.flashcard.model.service.BasicDAO<Productcategory> dao = new com.lt.flashcard.model.service.BasicDAO<Productcategory>(
                Productcategory.class);
        Criteria crit = dao.createCriteria();
        List<Productcategory> list = crit.list();
        for (Productcategory cat : list) {
            cat.setSlug(TextUtil.removeAccent(cat.getName()).toLowerCase());
            dao.save(cat);
        }

        Messagebox.show("Finish");
    }
}
